package com.prog3.calendarutec;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.prog3.calendarutec.beans.Evento;
import com.prog3.entidades.EntityEvento;

public class AddEvento extends Activity {
	
	public String x;
	
	public TextView mostrar;
	public EditText  fecha;
	static final int DATE_INI = 0;
	static final int DATE_FIN = 1;
	static final int HORA_INI = 2;
	static final int HORA_FIN = 3;
	
	private Button mPickTime; 
	private int mHourIni; 
	private int mMinuteIni; 
	static final int TIME_DIALOG_ID = 2;
	
	private Button mPickTimeFin; 
	private int mHourFin; 
	private int mMinuteFin; 
	static final int TIME_DIALOG_ID_FIN = 3;
	
	private EditText edtHoraIni;
	private EditText edtHoraFin;
	
	
	
	private int mYear; 
	private int mMonth; 
	private int mDay; 
	
	private int mYear2; 
	private int mMonth2; 
	private int mDay2; 
	
	private EditText edtFechaIni;
	private EditText edtFechaFin;
	
	// Para la fecha de inicio
	private DatePickerDialog.OnDateSetListener mDateListenerIni=
			new DatePickerDialog.OnDateSetListener() {
				
				public void onDateSet(DatePicker view, 
						int year, 
						int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					mYear=year;
					
					mMonth=monthOfYear;
					
					mDay=dayOfMonth;
					
					updateDisplay();
					
				}
			};
			
	
	// Para la fecha de fin
		private DatePickerDialog.OnDateSetListener mDateListenerFin=
				new DatePickerDialog.OnDateSetListener() {
					public void onDateSet(DatePicker view, 
								int year, 
								int monthOfYear,
								int dayOfMonth) {
							// TODO Auto-generated method stub
							mYear2=year;
							
							mMonth2=monthOfYear;
							
							mDay2=dayOfMonth;
							
							updateDisplay2();	
					}
		};	
			
		
	protected Dialog onCreateDialog(int id){ 
		switch(id){
			case DATE_INI:
					return new DatePickerDialog(this,this.mDateListenerIni,mYear,mMonth,mDay);
			
			case DATE_FIN:
					return new DatePickerDialog(this,this.mDateListenerFin,mYear2,mMonth2,mDay2);
			
			case HORA_INI:
				return new TimePickerDialog(this,mTimeListenerIni,this.mHourIni,this.mMinuteIni,false);
			case HORA_FIN:
				return new TimePickerDialog(this,mTimeListenerFin,this.mHourFin,this.mMinuteFin,false);
		}
		return null;
	}	
	
	private TimePickerDialog.OnTimeSetListener mTimeListenerIni=
			new TimePickerDialog.OnTimeSetListener(){

				public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
					// TODO Auto-generated method stub
					mHourIni=hourOfDay;
					mMinuteIni=minute;
					updateDisplayHour1();
				}
		
		
	}
			;
	
			private TimePickerDialog.OnTimeSetListener mTimeListenerFin=
					new TimePickerDialog.OnTimeSetListener(){

						public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
							// TODO Auto-generated method stub
							mHourFin=hourOfDay;
							mMinuteFin=minute;
							updateDisplayHour2();
						}
				
				
			};
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_evento);
        //fecha =(EditText)findViewById(R.id.fecha);
        final Calendar c =Calendar.getInstance();
        
        mYear=c.get(Calendar.YEAR);
        mMonth=c.get(Calendar.MONTH);
        mDay=c.get(Calendar.DAY_OF_MONTH);
        
        mYear2=c.get(Calendar.YEAR);
        mMonth2=c.get(Calendar.MONTH);
        mDay2=c.get(Calendar.DAY_OF_MONTH);
        
        mHourIni=c.get(Calendar.HOUR_OF_DAY);
        mMinuteIni=c.get(Calendar.MINUTE);

        mHourFin=c.get(Calendar.HOUR_OF_DAY);
        mMinuteFin=c.get(Calendar.MINUTE);

        
        
        edtFechaIni=(EditText) this.findViewById(R.id.edtFechaIni);
        edtFechaFin=(EditText) this.findViewById(R.id.edtFechaFin);
        edtHoraIni=(EditText) this.findViewById(R.id.edtHoraIni);
        edtHoraFin=(EditText) this.findViewById(R.id.edtHoraFin);
        
    }
    
    // Para la fecha de inicio
    public void updateDisplay(){
    	edtFechaIni.setText(new StringBuilder()
    			.append(mMonth+1).append("-")
    			.append(mDay).append("-")
    			.append(mYear).append(" ")
    			);
    }
    
    // Fecha de fin
    public void updateDisplay2(){
    	edtFechaFin.setText(new StringBuilder()
		.append(mMonth2+1).append("-")
		.append(mDay2).append("-")
		.append(mYear2).append(" ")
		);
    }
    
    public void updateDisplayHour1(){
    	this.edtHoraIni.setText(
    				new StringBuilder()
    				.append(pad(this.mHourIni))
    				.append(":")
    				.append(pad(this.mMinuteIni))
    	);
    	
    }
    
    public void updateDisplayHour2(){
    	
    	this.edtHoraFin.setText(
				new StringBuilder()
				.append(pad(this.mHourFin))
				.append(":")
				.append(pad(this.mMinuteFin))
	);
    }
   
    
    private static String pad(int c){
    	if(c>10)	
    		return String.valueOf(c);
    	else 
    		return "0"+String.valueOf(c);
    		
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add_evento, menu);
        return true;
    }
    
    // Listener de botones
    public void listenerFecha(View v){
    	
    	switch(v.getId())
    	{
    	case R.id.btnFechaIni:
    		showDialog(this.DATE_INI);
    		break;
    		
    	case R.id.btnFechaFin:
    		showDialog(this.DATE_FIN);
    		
    		break;
    	case R.id.btnHoraIni:
    		showDialog(this.TIME_DIALOG_ID);
    	break;
    	case R.id.btnHoraFin:
    		showDialog(this.TIME_DIALOG_ID_FIN);
    	break;
    	}
    	
    }
    
    public void add(View v){
    	EditText nombre= (EditText) this.findViewById(R.id.edtNombre);
    	EditText comentario= (EditText) this.findViewById(R.id.edtComentario);
    	EditText lugar= (EditText) this.findViewById(R.id.edtLugar);
    	EditText FechaIni= (EditText) this.findViewById(R.id.edtFechaIni);
    	EditText FechaFin= (EditText) this.findViewById(R.id.edtFechaFin);
    	EditText HoraIni= (EditText) this.findViewById(R.id.edtHoraIni);
    	EditText HoraFin= (EditText) this.findViewById(R.id.edtHoraFin);
    	
    	
    	Evento ev=new Evento();
    	
    	ev.setTitulo(nombre.getText().toString());
    	ev.setComentario(comentario.getText().toString());
    	ev.setLugar(lugar.getText().toString());
    	ev.setFecha_hora(FechaIni.getText().toString()+" "+HoraIni.getText().toString());
    	ev.setFecha_fin(FechaFin.getText().toString()+" "+HoraFin.getText().toString());
    	
    	ev.setId_Calendario(1);
    	
    	
    	EntityEvento evd=new EntityEvento(this);
    	evd.setBean(ev);
    	
    	if(evd.agregar()==0){
    		Toast.makeText(this, "No se agrego el evento", Toast.LENGTH_SHORT).show();
    		
    	}else{
    		Toast.makeText(this, "Evento agregado correctamente", Toast.LENGTH_SHORT).show();
    		
    	}
    
    	
    }
    
    
}
